package com.yunxin.admin.system.application.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunxin.admin.system.application.DictQueryService;
import com.yunxin.admin.system.application.assembler.DictAssembler;
import com.yunxin.admin.system.application.command.DictPageCommand;
import com.yunxin.admin.system.application.command.DictTypePageCommand;
import com.yunxin.admin.system.application.dto.*;
import com.yunxin.admin.system.infra.persistence.entity.SysDictDO;
import com.yunxin.admin.system.infra.persistence.entity.SysDictTypeDO;
import com.yunxin.admin.system.infra.persistence.mapper.SysDictMapper;
import com.yunxin.admin.system.infra.persistence.mapper.SysDictTypeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zero°
 * @since 2023-07-11
 */
@Service
@RequiredArgsConstructor
public class DictQueryServiceImpl implements DictQueryService {

    private final SysDictMapper sysDictMapper;

    private final SysDictTypeMapper sysDictTypeMapper;


    private final DictAssembler dictAssembler;

    @Override
    public Page<DictPageDTO> getDictPage(DictPageCommand command) {
        final String keywords = command.getKeywords();
        final String typeCode = command.getTypeCode();
        // 查询数据
        Page<SysDictDO> result =  sysDictMapper.selectPage(new Page<>(command.getPageNum(), command.getPageSize()),
                new LambdaQueryWrapper<SysDictDO>()
                        .like(CharSequenceUtil.isNotBlank(keywords), SysDictDO::getName, keywords)
                        .eq(CharSequenceUtil.isNotBlank(typeCode), SysDictDO::getTypeCode, typeCode)
                        .select(SysDictDO::getId, SysDictDO::getName, SysDictDO::getValue, SysDictDO::getStatus)
        );
        return dictAssembler.toDictPageDTO(result);
    }


    @Override
    public DictInfoDTO getDictInfoById(Long id) {
        // 获取sysDictDO
        SysDictDO sysDictDO = sysDictMapper.selectOne(new LambdaQueryWrapper<SysDictDO>()
                .eq(SysDictDO::getId, id)
                .select(
                        SysDictDO::getId,
                        SysDictDO::getTypeCode,
                        SysDictDO::getName,
                        SysDictDO::getValue,
                        SysDictDO::getStatus,
                        SysDictDO::getSort,
                        SysDictDO::getRemark
                ));
        Assert.isTrue(sysDictDO != null, "字典数据项不存在");
        return dictAssembler.toDictInfoDTO(sysDictDO);
    }

    @Override
    public List<OptionDTO<String>> listDictOptions(String typeCode) {
        // 数据字典项
        List<SysDictDO> dictList = sysDictMapper.selectList(new LambdaQueryWrapper<SysDictDO>()
                .eq(SysDictDO::getTypeCode, typeCode)
                .select(SysDictDO::getValue, SysDictDO::getName)
        );
        // 转换下拉数据
        return dictList.stream()
                .map(dictItem -> new OptionDTO<>(dictItem.getValue(), dictItem.getName()))
                .toList();
    }

    @Override
    public Page<DictTypePageDTO> getDictTypePage(DictTypePageCommand command) {
        final String keywords = command.getKeywords();
        // 查询数据
        Page<SysDictTypeDO> result = sysDictTypeMapper.selectPage(
                new Page<>(command.getPageNum(), command.getPageSize()),
                new LambdaQueryWrapper<SysDictTypeDO>()
                        .like(CharSequenceUtil.isNotBlank(keywords), SysDictTypeDO::getName, keywords)
                        .or()
                        .like(CharSequenceUtil.isNotBlank(keywords), SysDictTypeDO::getCode, keywords)
                        .select(SysDictTypeDO::getId, SysDictTypeDO::getName, SysDictTypeDO::getCode,
                                SysDictTypeDO::getStatus, SysDictTypeDO::getRemark
                        )
        );
        return dictAssembler.toDictTypePageDTO(result);
    }

    @Override
    public DictTypeInfoDTO getDictTypeById(Long id) {
        // 获取entity
        SysDictTypeDO sysDictTypeDO = sysDictTypeMapper.selectOne(new LambdaQueryWrapper<SysDictTypeDO>()
                .eq(SysDictTypeDO::getId, id)
                .select(SysDictTypeDO::getId, SysDictTypeDO::getName, SysDictTypeDO::getCode,
                        SysDictTypeDO::getStatus, SysDictTypeDO::getRemark
                ));
        Assert.isTrue(sysDictTypeDO != null, "字典类型不存在");
        return dictAssembler.toDictTypeInfoDTO(sysDictTypeDO);
    }


}
